#include "BinaryTree.h"

BinaryTree::BinaryTree()
{
    root = nullptr;
}

void BinaryTree::Insert(const ElementType &element)
{
    root = InsertNode(root, element);
}

void BinaryTree::Remove(const ElementType &element)
{
    root = RemoveNode(root, element);
}

BinaryTree::ElementType *BinaryTree::FindByElement(const ElementType &element)
{
    return FindElement(root, element);
}

void BinaryTree::show()
{
    std::cout << "前: ";
    PreTravel(root);
    std::cout << std::endl;
    std::cout << "中: ";
    MidTravel(root);
    std::cout << std::endl;
    std::cout << "后: ";
    BackTravel(root);
    std::cout << std::endl;
    std::cout << "层: ";
    HeightTravel(root);
}

BinaryTree::Node *BinaryTree::InsertNode(Node *node, const ElementType &element)
{
    // 如果直接找到要插入的位置
    if (node == nullptr)
        return new Node(element);
    // 要插入的元素已经存在 二叉排序树不放置相同数据
    if (node->data == element)
        return node;
    if (node->data > element)
    {
        // 递归插入左子树
        node->left = InsertNode(node->left, element);
        if (LRDevition(node) >= 2)
        {
            if (element < node->left->data)
                RotateRight(node);
            else
            {
                RotateLeftRight(node);
            }
        }
    }
    else
    {
        // 递归插入右子树
        node->right = InsertNode(node->right, element);
        if (LRDevition(node) <= -2)
        {
            if (element > node->right->data)
                RotateLeft(node);
            else
            {
                RotateRightLeft(node);
            }
        }
    }
    return node;
}

BinaryTree::Node *BinaryTree::RemoveNode(Node *node, const ElementType &element)
{
    if (node == nullptr)
        return nullptr;

    if (node->data < element)
    {
        node->right = RemoveNode(node->right, element);
    }
    else if (node->data > element)
    {
        node->left = RemoveNode(node->left, element);
    }
    else
    {
        // 叶子结点
        if (node->left == nullptr && node->right == nullptr)
        {
            delete node;
            return nullptr;
        }
        // 有两个孩子
        else if (node->left != nullptr && node->right != nullptr)
        {
            // 找到右子树中的最小值
            Node *minNode = GetMinNode(node->right);
            node->data = minNode->data; // 把最小值的结点数值给要被删除的结点
            // 删除右子树中的最小结点
            node->right = RemoveNode(node->right, minNode->data);
        }
        // 有一个孩子
        else
        {
            // 把子树接给被删掉的树的父树
            Node *child = node->left ? node->left : node->right;
            delete node;
            node = child;
        }
    }
    if (LRDevition(node) >= 2)
    {
        if(LRDevition(node->left)>0)
        {
            node= RotateRight(node);
        }
        else
        {
            node= RotateLeftRight(node);
        }
    }
    else if(LRDevition(node)<=-2)
    {
        if(LRDevition(node->right)<0)
        {
            node= RotateLeft(node);
        }
        else
        {
            node= RotateRightLeft(node);
        }
    }

    return node;
}

void BinaryTree::PreTravel(Node *node)
{
    if (node == nullptr)
        return;

    std::cout << node->data << " ";
    PreTravel(node->left);
    PreTravel(node->right);
}

void BinaryTree::MidTravel(Node *node)
{
    if (node == nullptr)
        return;

    MidTravel(node->left);
    std::cout << node->data << " ";
    MidTravel(node->right);
}

void BinaryTree::BackTravel(Node *node)
{
    if (node == nullptr)
        return;

    BackTravel(node->left);
    BackTravel(node->right);
    std::cout << node->data << " ";
}

void BinaryTree::HeightTravel(Node *node)
{
    LinkQueque q;
    q.Push(root);
    while (!q.IsEmpty())
    {
        Node *node = (Node *)*q.GetFront();
        std::cout << node->data << " ";
        if (node->left)
            q.Push(node->left);
        if (node->right)
            q.Push(node->right);
        q.Pop();
    }
    std::cout << std::endl;
}

BinaryTree::Node *BinaryTree::GetMinNode(Node *node)
{
    while (node->left != nullptr)
    {
        node = node->left;
    }
    return node;
}

BinaryTree::ElementType *BinaryTree::FindElement(Node *node, const ElementType &element)
{
    if (node == nullptr)
    {
        return nullptr;
    }
    if (element == node->data)
    {
        return &node->data;
    }
    else if (element < node->data)
    {
        return FindElement(node->left, element);
    }
    else
    {
        return FindElement(node->right, element);
    }
}

BinaryTree::Node *BinaryTree::RotateRight(Node *node)
{
    Node *p = node->left;
    node->left = p->right;
    p->right = node;
    return p; // 返回新的根
}

BinaryTree::Node *BinaryTree::RotateLeft(Node *node)
{
    Node *p = node->right;
    node->right = p->left;
    p->left = node;
    return p;
}

BinaryTree::Node *BinaryTree::RotateLeftRight(Node *node)
{
    // 左子树左旋
    node->left = RotateLeft(node->left);
    return RotateRight(node); // 整体右旋
}

BinaryTree::Node *BinaryTree::RotateRightLeft(Node *node)
{
    node->right = RotateRight(node->right);
    return RotateLeft(node);
}

int BinaryTree::GetNodeHight(Node *node)
{
    if (node == nullptr)
        return 0;

    int leftHight = GetNodeHight(node->left);

    int rightHight = GetNodeHight(node->right);

    return (leftHight > rightHight ? leftHight : rightHight);
}

int BinaryTree::LRDevition(Node *node)
{
    if (node == nullptr)
        return 0;
    return GetNodeHight(node->left) - GetNodeHight(node->right);
}
